﻿#region copyright
//<copyright>
// Copyright(C) 2012 TrackerRealm Corporation
// This file is part of the open source project - Jazz. http://jazz.codeplex.com
// 
// Jazz is open software: you can redistribute it and/or modify it 
// under the terms of the GNU Affero General Public License (AGPL) as published by 
// the Free Software Foundation, version 3 of the License.
// 
// Jazz is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty 
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
//  See the GNU Affero General Public License (AGPL) for more details.
// 
// You should have received a copy of the GNU General Public 
// License along with Jazz.  If not, see <http://www.gnu.org/licenses/>.
//
// REMOVAL OF THIS NOTICE IS VIOLATION OF THE COPYRIGHT. 
//</copyright>
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using System.Runtime.Serialization;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;
using TrackerRealm.Jazz.Client.Services;
using JsonObject = Newtonsoft.Json.Linq.JObject;
using JsonProperty = Newtonsoft.Json.Linq.JProperty;
using JsonArray = Newtonsoft.Json.Linq.JArray;
using JsonToken = Newtonsoft.Json.Linq.JToken;
using Newtonsoft.Json.Linq;
using F = TrackerRealm.Jazz.Common.Constants_JazzObjectFields;
using Field = TrackerRealm.Jazz.Client.Services.jObjectFields;
using TrackerRealm.Jazz.Common;
using TrackerRealm.Jazz.Client;
using TrackerRealm.Jazz.Json;

namespace TrackerRealm.Jazz.JsonSpecialized
{
    /// <summary>
    /// Provides a serialization scheme that allows for more flexibility.
    /// </summary>
    public static class JsonServices
    {
        #region DeserializeBaseJazz
        /// <summary>
        /// Returns a '<see cref="jObject"/>' with the base fields populated.
        /// *** Under construction ***
        /// </summary>
        /// <param name="jsonText"></param>
        /// <param name="serializer"></param>
        public static jObject Deserialize(string jsonText, JazzBaseFieldSerializer serializer)
        {
            JazzContainer container = JsonConvert.DeserializeObject<JazzContainer>(jsonText, serializer.Settings);
            return container.JazzObject;
        }
        #endregion
        #region JsonSerializeJazzBase Extension
        /// <summary>
        /// Returns a JSON string representing the base fields of this '<see cref="jObject"/>'.
        /// *** Under construction ***
        /// </summary>
        /// <param name="jazzObject"></param>
        /// <param name="serializer"></param>
        /// <returns></returns>
        public static string JsonSerializeJazzBase(this jObject jazzObject, JazzBaseFieldSerializer serializer)
        {
            return serializer.Serialize(jazzObject);
        }
        #endregion
        #region PopulateJazzBase
        /// <summary>
        /// 
        /// </summary>
        /// <param name="jazzObject"></param>
        /// <param name="jsonText"></param>
        /// <param name="serializer"></param>
        public static void PopulateJazzBase(this jObject jazzObject, string jsonText, JazzBaseFieldSerializer serializer)
        {
            jObject j = Deserialize(jsonText, serializer);
            List<MemberInfo> memberList = new List<MemberInfo>();
            List<object> dataList = new List<object>();
            Type t = j.GetType();
            do
            {
                FieldInfo[] members = t.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                foreach (FieldInfo m in members)
                {
                    if (!m.IsSerializeIgnoreAttribute() && m.IsBaseFieldAttribute())
                    {
                        memberList.Add(m);
                        dataList.Add(m.GetValue(j));
                    }
                }
                t = t.BaseType;
            } while(t != typeof(object));
            FormatterServices.PopulateObjectMembers(jazzObject, memberList.ToArray(), dataList.ToArray());
        }
        #endregion
    }
}
